/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DetailsPanel.java
 *
 * Created on May 3, 2010, 5:50:40 PM
 */

package org.dafe.partyband.gui.imports;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Map;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import org.dafe.partyband.gui.Utilities;
import org.dafe.partyband.importer.api.IImportRecipe;
import org.dafe.partyband.importer.api.IMimeTypeDetector;
import org.dafe.partyband.importer.api.ISongImport;
import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.color.ColorUtil;
import org.openide.util.NbBundle;

/**
 *
 * @author dafe
 */
public final class DetailsPanel extends javax.swing.JPanel {
    private NameAuthorTable nameAuthorTable;
    private volatile boolean preparing;
    private final DetailsWizardPanel wizardPanel;
    private SongResourcesTable resourcesTable;
    private IssuesList issuesList;

    /** Creates new form DetailsPanel */
    public DetailsPanel(DetailsWizardPanel wizardPanel) {
        this.wizardPanel = wizardPanel;
        initComponents();

        showPreparing();
    }

    @Override
    public String getName() {
        return NbBundle.getMessage(ChooseDirPanel.class, "LBL_DetailsPanel");
    }
    
    public void showPreparing () {
        boolean oldValue = preparing;
        preparing = true;
        if (oldValue != preparing) {
            wizardPanel.fireChangeEvent();
        }

        removeAll();
        add(new PreparingProgressPanel());
        revalidate();
        repaint();
    }

    public void showImport (final IImportRecipe recipe, IMimeTypeDetector mimeDetector) {
        removeAll();

        issuesList = new IssuesList();
        resourcesTable = new SongResourcesTable(mimeDetector);
        nameAuthorTable = new NameAuthorTable(recipe, issuesList, resourcesTable);

        final JXMultiSplitPane msp = new JXMultiSplitPane();
        StringBuilder sb = new StringBuilder();
        sb.append("(COLUMN (ROW weight=0.8");
        sb.append("(LEAF name=nameAuthor weight=0.5) (LEAF name=resources weight=0.5))");
        sb.append("(LEAF name=issues weight=0.2))");

        MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(sb.toString());
        msp.getMultiSplitLayout().setModel(modelRoot);
        msp.add(nameAuthorTable, "nameAuthor");
        msp.add(resourcesTable, "resources");
        msp.add(issuesList, "issues");
        //msp.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        msp.setDividerSize(8);
        msp.setDividerPainter(Utilities.createDividerPainter(msp));
        
        add(msp, BorderLayout.CENTER);

        nameAuthorTable.getTable().getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent evt) {
                ListSelectionModel selModel = nameAuthorTable.getTable().getSelectionModel();
                TableModel model = nameAuthorTable.getTable().getModel();
                final int index = selModel.getMinSelectionIndex();
                if (index != -1) {
                    int modelIndex = nameAuthorTable.getTable().convertRowIndexToModel(index);
                    ISongImport songImport = recipe.getSongImports().get(modelIndex);
                    resourcesTable.setSongImport(songImport,
                            nameAuthorTable.getEnabledMap().get(songImport));
                    issuesList.setContent(songImport.getEvaluation().getIssues(),
                            songImport.getSongToImport().getName());

                }
            }
        });

        revalidate();
        repaint();

        boolean oldValue = preparing;
        preparing = false;
        if (oldValue != preparing) {
            wizardPanel.fireChangeEvent();
        }
    }

    public boolean isPreparing() {
        return preparing;
    }

    public ImportResult getResult () {
        return new ImportResult(nameAuthorTable.getRecipe(), nameAuthorTable.getEnabledMap());
    }

    public static final class ImportResult {
        IImportRecipe recipe;
        Map<ISongImport, Boolean> importEnabled;

        public ImportResult(IImportRecipe recipe, Map<ISongImport, Boolean> importEnabled) {
            this.recipe = recipe;
            this.importEnabled = importEnabled;
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables


}
